package com.wn.framework.adapter

import android.util.SparseArray
import androidx.core.util.size
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter

/**
 * @Author: wn
 * @Date: 2024/10/15 16:54
 * @Description: ViewPage2 的 Adapter
 */

class ViewPage2FragmentAdapter(
  fragmentManager: FragmentManager,
  lifecycle: Lifecycle,
  var fragments: SparseArray<Fragment>,
) : FragmentStateAdapter(fragmentManager, lifecycle) {

  override fun getItemCount(): Int = fragments.size

  // FragmentStateAdapter内部自己会管理已实例化的 fragment 对象，所以不需要考虑复用的问题
  override fun createFragment(position: Int): Fragment = fragments[position]

  // 传递数据 fragments
  fun setData(_fragments: SparseArray<Fragment>) {
    fragments = _fragments
  }
}